package dao;

import common.pojo.Page;
import common.spring.jdbc.MySQLHelper;
import common.utils.MyMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import pojo.ChatList;
import pojo.form.ChatListAddForm;
import pojo.form.ChatListQueryForm;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by asus on 2018/11/22.
 */
@Repository
public class ChatListDao {
    @Autowired
    MySQLHelper mySQLHelper;

    public int addCharList(ChatListAddForm addForm ){
        return mySQLHelper.simpleInsert("chat_list", MyMapUtils.beanToMap(addForm,false));
    }
    public int addCharRecordExist(ChatListAddForm addForm){
        StringBuilder builder = new StringBuilder();
        builder.append(" insert into chat_list(photo,shopId,nickName,customId,createTime) ");
        builder.append(" SELECT   ");
        builder.append("'"+addForm.getPhoto()+"',");
        builder.append("'"+addForm.getShopId()+"',");
        builder.append("'"+addForm.getNickName()+"',");
        builder.append("'"+addForm.getCustomId()+"',");
        builder.append("sysdate()");
        builder.append(" FROM DUAL");
        builder.append(" where not EXISTS( ");
        builder.append(" select id from chat_list where customId=? and shopId=? )");
        List list = new ArrayList();

        list.add(addForm.getCustomId());
        list.add(addForm.getShopId());
        return mySQLHelper.insert(builder.toString(),list.toArray());
    }
    public int updateChatList(String message,Long shopId,Long customId){
        return mySQLHelper.update("update chat_list set message=?,nums=nums+1,messageTime=? where shopId=? and customId = ? ",new Object[]{message,new Date(),shopId,customId});
    }

    public Page<ChatList> getChatList(ChatListQueryForm queryForm, int curPage, int pageSize){
        StringBuilder stringBuilder = new StringBuilder();
        List list = new ArrayList();
        stringBuilder.append(" select a.*  from chat_list a ");
        stringBuilder.append(" where 1=1 ");
        if(queryForm.getCustomId()!=null && queryForm.getCustomId()>0){
            stringBuilder.append(" and a.customId = ? ");
            list.add(queryForm.getCustomId());
        }
        if(queryForm.getShopId()!=null && queryForm.getShopId()>0){
            stringBuilder.append(" and a.shopId = ? ");
            list.add(queryForm.getShopId());
        }
        stringBuilder.append(" order by a.id desc");
        return mySQLHelper.queryForPageBean(stringBuilder.toString(),list.toArray(),ChatList.class,curPage,pageSize);
    }


    public int updateChatList(Long customId,Long shopId){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(" update chat_list set nums=0 where customId=? and shopId=?");
        return mySQLHelper.update(stringBuilder.toString(),new Object[]{customId,shopId});
    }

    public Long getUnRradMsg(Long shopId){
        StringBuilder stringBuilder  = new StringBuilder();
        stringBuilder.append(" select count(1) from  chat_list where shopId= ? and nums>0 ");
        return mySQLHelper.queryForNumber(stringBuilder.toString(),new Object[]{shopId});
    }
}
